<template>
  <div class="app-container">
    <div class="head">
      <img src="@/assets/img/Layout/position.png" />
      <p class="see-block">供应商</p>
    </div>
    <div class="view-container">
      <el-form :model="queryParams" ref="queryForm" :inline="true">
        <el-form-item label="供应商名称" prop="searchValue">
          <el-input
            v-model="queryParams.searchValue"
            placeholder="请输入供应商名称检索"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery" class="list-search btn-success" color="#5ED4D4">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery" class="list-search">重置</el-button>
        </el-form-item>
      </el-form>
      <el-table v-loading="loading" :data="supplierList" @row-click="handleView" :default-sort="{prop: 'date', order: 'descending'}" border>
        <el-table-column label="序号" type="index" align="center" width="50">
           <template slot-scope="scope">
            <span>{{(queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1}}</span>
          </template>
        </el-table-column>
        <el-table-column label="编号" align="center" prop="supCode" :show-overflow-tooltip="true" width="200" sortable />
        <el-table-column label="名称" align="center" prop="name" :show-overflow-tooltip="true" width="200" />
        <el-table-column label="简称" align="center" prop="shortName"  :show-overflow-tooltip="true" />
        <el-table-column label="助记码" align="center" prop="mnemonicCode" :show-overflow-tooltip="true" />
        <el-table-column label="供应商分类" align="center" prop="sortId" :show-overflow-tooltip="true" />
        <el-table-column label="纳税比例类型" align="center" :show-overflow-tooltip="true" width="120">
          <template slot-scope="scope">
            {{ scope.row.shareInvoice == null ? '' : scope.row.shareInvoice.taxpayerScale == '0' ? '13%' : scope.row.shareInvoice.taxpayerScale == '1' ? '9%' : scope.row.shareInvoice.taxpayerScale == '2' ? '6%' : scope.row.shareInvoice.taxpayerScale == '3' ? '3%' : scope.row.shareInvoice.taxpayerScale == '4' ? '1%' : '' }}
          </template>
        </el-table-column>
        <!-- <el-table-column label="营业执照编号" align="center" prop="businessLicense" :show-overflow-tooltip="true" /> -->
         <el-table-column label="审核状态" align="center" prop="approveStatus">
          <template slot-scope="scope">
            <div v-if="scope.row.approveStatus == 0" style="color:#13C2C2">待审核</div>
            <div v-if="scope.row.approveStatus == 1" style="color:#52C41A">已审核</div>
            <div v-if="scope.row.approveStatus == 3" style="color:#fd562c">拒绝</div>
          </template>
        </el-table-column>
        <el-table-column label="状态" align="center" prop="status">
          <template slot-scope="scope">
            <div v-if="scope.row.status == 2" style="color:#fd562c">禁用</div>
            <div v-if="scope.row.status == 1" style="color:#52C41A">正常</div>
          </template>
        </el-table-column>
        <el-table-column label="备注" align="center" prop="remark" :show-overflow-tooltip="true" />
      </el-table>
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
  </div>
</template>

<script>
import { listSupplier } from "@/api/system/basicdata/basicdata"
import { listSupplierlist } from "@/api/system/basedata/supplierlist/supplierlist"

export default {
  name: "supplier",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 生产列表数据
      supplierList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        approveStatus: 1,
        status: 1,
        searchValue:undefined
      },
      type: '',
      id: '',
    }
  },
  created() {
    if(this.$route.query.type){
      this.type = this.$route.query.type
      this.id = this.$route.query.id
      this.list = this.$route.query.list
      this.getSearchList()
    }else{
      this.getList()
    }
  },
  methods: {
    // 获取列表
    getList() {
      this.loading = true
      listSupplier(this.addDateRange(this.queryParams)).then(res => {
        this.supplierList = res.rows
        this.total = res.total
        this.loading = false
        this.getSupplierlist()
      })
    },
    // 筛选列表
    getSearchList(){
      this.loading = true
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        status: 1, 
        approvalStatus: 1,
        searchValue:undefined
      },
      listSupplier(this.addDateRange(this.queryParams)).then(res => {
        this.supplierList = res.rows
        this.total = res.total
        this.loading = false
        this.getSupplierlist()
      })
    },
    getSupplierlist(){
      listSupplierlist().then(res => {
        this.supplierList.forEach(item => {
          let arr = res.rows.find(e =>{
            return e.id == item.sortId
          })
          // console.log(arr)
          if(arr != undefined){
            item.sortId = arr.sortName
          }
        })
      })
    },
    // 搜索按钮
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    // 重置按钮
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 查看详情
    handleView(row) {
      if(this.type == "1"){
        if(row.status == '1' && row.approveStatus == "1"){
          this.$tab.closeOpenPage({ path: "/purchase/purchaseOrder/add", query:{ supplier: row } })
        }else{
          this.$modal.msgError('请选择已审核的供应商！')
        }
      }else if(this.type == 'supplier'){
        if(row.status == '1' && row.approveStatus == "1"){
          this.$tab.closeOpenPage({ path: "/purchase/purchaseOrder/edit", query:{ id: this.id, type: this.type, supplier: row, list: this.list } })
        }else{
          this.$modal.msgError('请选择已审核的供应商！')
        }
      }else if(this.type == 'purchaseReturnAdd'){
        if(row.status == '1' && row.approveStatus == "1"){
          this.$tab.closeOpenPage({path:"/purchase/purchaseReturn/add",query:{ supplier: row}})
        }else{
          this.$modal.msgError('请选择已审核的供应商！')
        }
      }else if(this.type == 'purchaseReturnEdit'){
        if(row.status == '1' && row.approveStatus == "1"){
          this.$tab.closeOpenPage({ path: "/purchase/purchaseReturn/edit", query:{ id: this.id, supplier: row, list: this.list } })
        }else{
          this.$modal.msgError('请选择已审核的供应商！')
        }
      }else{
        this.$router.push({path:"/basicData/detail",query:{data: row, type: 'supplier'}})
      }
    }
  }
}
</script>